package main

import (
	"fmt"
	"time"
)

// main
//
//	@Description: 默认情况：管道可读可写
func main() {
	channelOnlyWrite()

	channelOnlyRead()

	channelOnlyDemo()
}

// channelOnlyDemo
//
//	@Description: 读写数据demo
func channelOnlyDemo() {
	var ch chan int = make(chan int, 10)
	exitChan := make(chan struct{}, 2)
	go send(ch, exitChan)
	go rev(ch, exitChan)
	for i := 0; i < 2; i++ {
		<-exitChan
	}
	fmt.Println("main...over...")
}

// send
//
//	@Description: 只能写，不能读
//	@param ch
//	@param exitChan
func send(ch chan<- int, exitChan chan struct{}) {
	for i := 0; i < 10; i++ {
		fmt.Println(">>>>写入数据=", i)
		ch <- i
		time.Sleep(time.Millisecond * 500)
	}
	close(ch)
	var a struct{}
	exitChan <- a
}

// rev
//
//	@Description: 只能读，不能写
//	@param ch
//	@param exitChan
func rev(ch <-chan int, exitChan chan struct{}) {
	for {
		v, ok := <-ch
		if !ok {
			break
		}
		fmt.Println("<<<<读取数据=", v)
	}
	var a struct{}
	exitChan <- a
}

// channelOnlyWrite
//
//	@Description:
//	@Description: 只能写，不能读
func channelOnlyWrite() {
	ch := make(chan<- int, 3)
	ch <- 10
	//num := <-ch //Invalid operation: <-ch (receive from the send-only type chan<- int)
}

// channelOnlyRead
//
//	@Description: 只能读，不能写
func channelOnlyRead() {
	ch := make(<-chan int, 3)
	num := <-ch
	fmt.Println("num=", num)
}
